package com.zhao.service.serviceImpl;

import com.zhao.Others.DateAndTimeObject;
import com.zhao.Others.FileOperationObject;
import com.zhao.Others.GenerateObject;
import com.zhao.dao.*;
import com.zhao.pojo.*;
import com.zhao.service.DrivingLicenseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ClassUtils;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class DrivingLicenseServiceImpl implements DrivingLicenseService {

    @Autowired
    DrivingLicenseDao drivingLicenseDao;

    @Autowired
    StudentDao studentDao;

    @Autowired
    EbickDao ebickDao;

    @Autowired
    DepartmentDao departmentDao;

    @Autowired
    UniversityDao universityDao;

    @Autowired
    SuperviseDao superviseDao;

    @Autowired
    SchoolCounsellorDao schoolCounsellorDao;

    //////////////////////////////   admin       ///////////////////////////////////////
    @Override
    public String adminGetAllDL(Admin admin, HttpSession session) {

        List<DrivingLicense> drivingLicenseList = drivingLicenseDao.findAll(admin.getUniversityID());

        System.out.println(drivingLicenseList.size());

        List<DrivingLicenseImpl> drivingLicenseList1 = new ArrayList<>();

        for(DrivingLicense drivingLicense : drivingLicenseList){

            Student student = studentDao.findByStudentID(drivingLicense.getOwnerID(),admin.getUniversityID());
            if(student == null)continue;
            DrivingLicenseImpl drivingLicense1 = formDrivingLicenseImpl(student,drivingLicense);
            drivingLicenseList1.add(drivingLicense1);
        }
        session.setAttribute("adminAllDL",drivingLicenseList1);

        return "success";
    }

    @Override
    public String adminGetOneDL(Admin admin, String DLID, HttpSession session) {

        DrivingLicense drivingLicense = drivingLicenseDao.findByID(DLID,admin.getUniversityID());
        if(drivingLicense == null){
            return "行驶证号错误";
        }
        Student student = studentDao.findByStudentID(drivingLicense.getOwnerID(),admin.getUniversityID());
        DrivingLicenseImpl drivingLicense1 = formDrivingLicenseImpl(student,drivingLicense);
        session.setAttribute("adminOneDL",drivingLicense1);
        return "success";
    }


    ////////////////////////////   公共模块   ////////////////////////////////////////////

    //形成DrivingLicenseImpl   通过student 和 drivinglicense
    private DrivingLicenseImpl formDrivingLicenseImpl(Student student,DrivingLicense drivingLicense){

        DrivingLicenseImpl drivingLicenseImpl = new DrivingLicenseImpl();
        drivingLicenseImpl.setStudentName(student.getName());
        drivingLicenseImpl.setStudentID(student.getStudentID());

        drivingLicenseImpl.setGenerateTime(drivingLicense.getGenerateTime());
        drivingLicenseImpl.setDrivingLicenseID(drivingLicense.getDrivingLicenseID());
        drivingLicenseImpl.setCancelReason(drivingLicense.getCancelReason());
        drivingLicenseImpl.setRejectReason(drivingLicense.getRejectReason());
        Ebick ebick = ebickDao.findByID(drivingLicense.getEbickID(),drivingLicense.getUniversityID());
        if(ebick == null){
            return  null;
        }
        drivingLicenseImpl.setImage(ebick.getImage());
        drivingLicenseImpl.setBrand(ebick.getBrand());
        drivingLicenseImpl.setStatus(drivingLicense.getStatus());
        drivingLicenseImpl.setEbickID(drivingLicense.getEbickID());

        Department department = departmentDao.findByID(student.getDepartmentID(),student.getUniversityID());
        if(department == null){
            return null;
        }
        drivingLicenseImpl.setDepartment(department.getDepartmentName());

        University university = universityDao.findByID(student.getUniversityID());
        drivingLicenseImpl.setUniversityName(university.getUniversityName());

        SchoolCounsellor schoolCounsellor = schoolCounsellorDao.findByID(drivingLicense.getApproverID(),student.getUniversityID());

        if(schoolCounsellor != null){
            drivingLicenseImpl.setApproverID(schoolCounsellor.getSchoolCounsellorID());
            drivingLicenseImpl.setApproverName(schoolCounsellor.getName());
        }


        return drivingLicenseImpl;
    }



    ////////////////////////// 校警 ////////////////////////////////////


    @Override
    public String superviserFindDrivingLicense(String ebickID, HttpSession session) {

        Superviser superviser = (Superviser)session.getAttribute("superviser");
        if(superviser==null){
            System.out.println("请先登录");
            return "error";
        }

        Ebick ebick = ebickDao.findByID(ebickID,superviser.getUniversityID());
        if(ebick==null){
            System.out.println("电动车牌照不存在");
            return "电动车牌照不存在";
        }

        DrivingLicense drivingLicense = drivingLicenseDao.findByEbickID(ebickID,superviser.getUniversityID());
        if(drivingLicense==null){
            System.out.println("查询行驶证失败");
            return "查询行驶证失败";
        }

        Student student = studentDao.findByStudentID(drivingLicense.getOwnerID(),superviser.getUniversityID());

        DrivingLicenseImpl drivingLicenseImpl = formDrivingLicenseImpl(student,drivingLicense);

        session.setAttribute("scanEbick",ebick);
        session.setAttribute("drivingLicenseImpl",drivingLicenseImpl);


        return "success";
    }

    //////////////////////////// 辅导员  ///////////////////////////////////




    //驳回行驶证申请
    @Override
    public String rejectDrivingLicenseApplication(String drivingLicenseID, String rejectReason, HttpSession session) {

        SchoolCounsellor schoolCounsellor = (SchoolCounsellor)session.getAttribute("schoolCounsellor") ;
        if(schoolCounsellor == null){
            System.out.println("请登录");
            return "error";
        }

        DrivingLicense drivingLicense = drivingLicenseDao.findByID(drivingLicenseID,schoolCounsellor.getUniversityID());
        if(drivingLicense==null){
            System.out.println("行驶证号错误");
            return "error";
        }

        Ebick ebick =ebickDao.findByID(drivingLicense.getEbickID(),schoolCounsellor.getUniversityID());
        ebick.setStatus("已驳回");
        ebick.setReason(rejectReason);
        int res = ebickDao.updateStatusAndReason(ebick);
        if(res !=1){
            System.out.println("更新电动车错误");
            return "error";
        }

        drivingLicense.setRejectReason(rejectReason);

        res = drivingLicenseDao.deleteByID(drivingLicenseID,schoolCounsellor.getUniversityID());
        if(res != 1){
            System.out.println("更新行驶证错误");
            return "error";
        }



        return "success";
    }


    //驳回注销
    @Override
    public String rejectCancelDrivingLicense(String drivingLicenseID, HttpSession session) {

        SchoolCounsellor schoolCounsellor = (SchoolCounsellor)session.getAttribute("schoolCounsellor") ;
        if(schoolCounsellor == null){
            System.out.println("请登录");
            return "error";
        }

        DrivingLicense drivingLicense = drivingLicenseDao.findByID(drivingLicenseID,schoolCounsellor.getUniversityID());
        if(drivingLicense==null){
            System.out.println("行驶证号错误");
            return "error";
        }

        drivingLicense.setStatus("正常");

        int res = drivingLicenseDao.update(drivingLicense);
        if(res!=1){
            System.out.println("更新行驶证错误");
            return "error";
        }
        return "success";
    }


    //批准注销
    @Override
    public String approveCancelDrivingLicense(String drivingLicenseID, HttpSession session) {

        SchoolCounsellor schoolCounsellor = (SchoolCounsellor)session.getAttribute("schoolCounsellor") ;
        if(schoolCounsellor == null){
            System.out.println("请登录");
            return "error";
        }

        DrivingLicense drivingLicense = drivingLicenseDao.findByID(drivingLicenseID,schoolCounsellor.getUniversityID());
        if(drivingLicense==null){
            System.out.println("行驶证号错误");
            return "error";
        }

        Ebick ebick = ebickDao.findByID(drivingLicense.getEbickID(),drivingLicense.getUniversityID());
        if(ebick == null){
            System.out.println("ebick 不存在");
            return "error";
        }
        ebick.setStatus("无证");


        int res = drivingLicenseDao.deleteByID(drivingLicenseID,schoolCounsellor.getUniversityID());
        if(res != 1){
            return "error";
        }

        int res1 = ebickDao.updateStatus(ebick);
        if(res1 != 1){
            System.out.println("电动车更新出错");
            return "error";
        }

        return "success";
    }


    //查看 所辖 学生的  行驶证申请
    @Override
    public String getApplicationOfDrivingLicense(SchoolCounsellor schoolCounsellor,HttpSession session) {

        //1 找到所辖 所有  有电动车 学生
        System.out.println("查找所辖 所有  有电动车 学生");
        List<Student> studentList = studentDao.findByUniversityIDAndDepartmentIDAndMajorIDAndEbickAmountBiggerThan(
                schoolCounsellor.getUniversityID(),
                schoolCounsellor.getDepartmentID(),
                schoolCounsellor.getMajorID(),
                0);

        System.out.println("找到 "+studentList.size()+" 个学生");
        System.out.println(studentList);

        //2 按照学号 挨个寻找 申请 组成链表
        System.out.println("查找学校内 所有  申请待批准的 行驶证明");
        List<DrivingLicense> drivingLicenseList = drivingLicenseDao.findByStatusAndUniversityID("申请待批准",schoolCounsellor.getUniversityID());
        System.out.println(drivingLicenseList);

        System.out.println("比对学号 组成新的链表");

        List<DrivingLicenseImpl> applicationList = new ArrayList<>();

        for(int i=0;i<studentList.size();i++){

            Student student = studentList.get(i);

            for(int j=0;j<drivingLicenseList.size();j++){

                DrivingLicense drivingLicense = drivingLicenseList.get(j);

                if(student.getStudentID().equals(drivingLicense.getOwnerID())){

                    DrivingLicenseImpl drivingLicenseImpl = formDrivingLicenseImpl(student,drivingLicense);

                    applicationList.add(drivingLicenseImpl);
                }
            }
        }
        System.out.println("找到了 "+applicationList.size()+"个符合要求的申请");
        System.out.println(applicationList);

        session.setAttribute("applicationList",applicationList);

        return "success";
    }

    @Override
    public String approveDrivingLicense(String drivingLicenseID, HttpSession session) {

        SchoolCounsellor schoolCounsellor = (SchoolCounsellor)session.getAttribute("schoolCounsellor") ;
        if(schoolCounsellor == null){
            System.out.println("请登录");
            return "error";
        }
        System.out.println("查询行驶证");
        DrivingLicense drivingLicense = drivingLicenseDao.findByID(drivingLicenseID,schoolCounsellor.getUniversityID());

        System.out.println("查询电动车");
        Ebick ebick = ebickDao.findByID(drivingLicense.getEbickID(),schoolCounsellor.getUniversityID());

        if(ebick == null){
            System.out.println("无此电动车");
            return "error";
        }

        System.out.println("修改行驶证状态");
        drivingLicense.setStatus("正常");
        drivingLicense.setGenerateTime(DateAndTimeObject.getNowDate()+" "+DateAndTimeObject.getNowTime());
        drivingLicense.setApproverID(schoolCounsellor.getSchoolCounsellorID());

        System.out.println("修改电动车状态");
        ebick.setStatus("有证");

        int res = drivingLicenseDao.update(drivingLicense);

        if(res!=1){
            System.out.println("更新行驶证操作失败");
            return "error";
        }

        res = ebickDao.updateStatus(ebick);
        if(res!=1){
            System.out.println("更新电动车状态操作失败");
            return "error";
        }


        return "success";
    }


    //辅导员 查看所有学生撤销申请
    @Override
    public String getCancelDrivingLicenseApplication(HttpSession session) {

        SchoolCounsellor schoolCounsellor =(SchoolCounsellor)session.getAttribute("schoolCounsellor") ;
        if(schoolCounsellor==null){
            System.out.println("请先登录");
            return "error";
        }

        List<DrivingLicense> licenseList = drivingLicenseDao.findByStatusAndApproverIDAndUniversityID(
                "注销待批准",schoolCounsellor.getSchoolCounsellorID(),schoolCounsellor.getUniversityID());

        if(licenseList==null){
            System.out.println("没有申请");
        }

        List<DrivingLicenseImpl> drivingLicenseList = new ArrayList<>();

        for(int i=0 ; i<licenseList.size();i++){

            DrivingLicense drivingLicense = licenseList.get(i);

            Student student = studentDao.findByStudentID(drivingLicense.getOwnerID(),schoolCounsellor.getUniversityID());

            DrivingLicenseImpl drivingLicenseImpl = formDrivingLicenseImpl(student,drivingLicense);

            drivingLicenseList.add(drivingLicenseImpl);

        }


        session.setAttribute("drivingLicenseList",drivingLicenseList);

        return "success";
    }

    //////////////////////////// 学生  ///////////////////////////////////


    //学生
    //撤销行驶证申请
    @Override
    public String cancleApplyForDrivingLicense(String ebickID, HttpSession session) {

        Student student = (Student)session.getAttribute("student");
        if(student==null){
            System.out.println("请先登录");
            return "error";
        }

        Ebick ebick = ebickDao.findByID(ebickID,student.getUniversityID());

        if(!ebick.getStatus().equals("申请行驶证明待批准")){
            session.setAttribute("applyForMessage","没有可撤销的申请");
            return "没有可撤销的申请";
        }

        ebick.setStatus("无证");

        int res = ebickDao.updateStatus(ebick);
        if(res!=1){
            System.out.println("更新失败");
            return "error";
        }

        DrivingLicense drivingLicense = drivingLicenseDao.findByStudentID(student.getStudentID(),student.getUniversityID());
        res = drivingLicenseDao.deleteByID(drivingLicense.getDrivingLicenseID(),student.getUniversityID());
        if(res != 1){
            System.out.println("删除行驶证失败");
            return "error";
        }

        return "success";
    }

    //学生
    ////查看自己的形式证明
    @Override
    public String getDrivingLicenseImpl(HttpSession session) {
        System.out.println("查看行驶证明");
        Student student = (Student) session.getAttribute("student");

        DrivingLicense drivingLicense = drivingLicenseDao.findByStudentID(student.getStudentID(),student.getUniversityID());
        System.out.println(drivingLicense);
        if(drivingLicense == null){
            session.setAttribute("drivingLicenseImpl",null);
            session.setAttribute("getDrivingLicenseMessage","没有行驶证");
            return "没有行驶证";
        }

        DrivingLicenseImpl drivingLicenseImpl = formDrivingLicenseImpl(student,drivingLicense);
        System.out.println(drivingLicenseImpl);

        if(drivingLicenseImpl!=null){
            session.setAttribute("drivingLicenseImpl",drivingLicenseImpl);
        }

        return "success";
    }


    @Override
    public String toCancel(String drivingLicenseID, HttpSession session) {

        Student student = (Student)session.getAttribute("student");
        if(student==null){
            System.out.println("请先登录");
            return "error";
        }

        DrivingLicense drivingLicense = drivingLicenseDao.findByID(drivingLicenseID,student.getUniversityID());
        if(drivingLicense==null){
            System.out.println("行驶证号码错误");
            return "error";
        }

        if(!drivingLicense.getStatus().equals("正常")){
            System.out.println("当前不能注销");
            return "当前不能注销";
        }

        return "可以注销";

    }


    //学生
    //注销行驶证
    @Override
    public String cancelDrivingLicense(String drivingLicenseID,String cancelReason,HttpSession session) {

        System.out.println("注销行驶证");
        Student student = (Student)session.getAttribute("student");
        if(student==null){
            System.out.println("请先登录");
            return "error";
        }

        //System.out.println(student);
        DrivingLicense drivingLicense = drivingLicenseDao.findByID(drivingLicenseID,student.getUniversityID());
        //System.out.println(drivingLicense);
        if(drivingLicense==null){

            System.out.println("行驶证明号码错误");
            return "error";
        }
        drivingLicense.setStatus("注销待批准");
        drivingLicense.setCancelReason(cancelReason);
        int res = drivingLicenseDao.update(drivingLicense);
        if(res!=1){
            System.out.println("填写注销理由失败");
            return "error";
        }

        System.out.println("申请成功");
        return "success";
    }

    //学生
    //申请行驶证
    @Override
    public String applyForDrivingLicense(String ebickID, HttpSession session) {

        System.out.println("申请行驶证");
        Student student = (Student)session.getAttribute("student");
        if(student == null){
            //System.out.println("请先登录");
            return "请先登录";
        }

        ////！！！！查看是否有正在申请中的行驶证  或者 有正常的行驶证
        DrivingLicense drivingLicense1 = drivingLicenseDao.findByStudentID(student.getStudentID(),student.getUniversityID());
        if(drivingLicense1!=null){
            System.out.println("当前不可以申请行驶证");
            session.setAttribute("applyForMessage","当前不可以申请行驶证");
            return "当前不可以申请行驶证";
        }


        Ebick ebick = ebickDao.findByID(ebickID,student.getUniversityID());
        if(ebick.getStatus().equals("已发布")){
            System.out.println("电动车已发布");
            session.setAttribute("applyForMessage","电动车已发布");
            return "电动车已发布";
        }

        System.out.println("生成行驶证");
        //生成行驶证号 //生成行驶证
        DrivingLicense drivingLicense = GenerateObject.generateDrivingLicense(student,ebick);
        drivingLicense.setStatus("申请待批准");
        System.out.println(drivingLicense);

        ///生成行驶证明后  为  申请行驶证明待批准  状态
        ebick.setStatus("申请行驶证明待批准");
        int res = ebickDao.updateStatus(ebick);
        if(res != 1){
            //System.out.println("更新电动车信息错误");
            return "更新电动车信息错误";
        }

        //行驶证信息  保存到数据库
        int res1 = drivingLicenseDao.add(drivingLicense);

        if(res1 != 1){
            System.out.println("保存行驶证信息错误");
            return "保存行驶证信息错误";
        }
        System.out.println("行驶证保存成功，待批准");

        return "success";
    }


//////////////////////////////////
    //学生  没用！！！！！！
    @Override
    public List<DrivingLicenseImpl> getDrivingLicenseImplByStudentID(String studentID) {



        return null;
    }

    //学生  没用！！！！
    /////通过ID找
    @Override
    public DrivingLicenseImpl getDrivingLicenseImplByID(String drivingLicenseID , HttpSession session) {

        DrivingLicenseImpl drivingLicenseImpl = new DrivingLicenseImpl();
        System.out.println(drivingLicenseID);
        String universityID = ((Student) session.getAttribute("student")).getUniversityID();
        System.out.println(universityID);
        DrivingLicense drivingLicense = drivingLicenseDao.findByID(drivingLicenseID,universityID);
        System.out.println(drivingLicense);
        if(drivingLicense==null){
            System.out.println("行驶证不存在");
            return null;
        }

        //!@!!!!!!!!!!!!!!!!!1
        Ebick ebick = ebickDao.findByID(drivingLicense.getEbickID(),universityID);
        if(ebick==null){
            System.out.println("电动车牌照不存在");
            return null;
        }

        Student student = studentDao.findByStudentID(drivingLicense.getOwnerID(),universityID);
        if(student==null){
            System.out.println("学号不存在");
            return null;
        }

        drivingLicenseImpl.setDrivingLicenseID(drivingLicenseID);
        drivingLicenseImpl.setGenerateTime(drivingLicense.getGenerateTime());
        drivingLicenseImpl.setBrand(ebick.getBrand());
        drivingLicenseImpl.setStudentName(student.getName());
        drivingLicenseImpl.setImage(ebick.getImage());

        ///查询 学院  填 学院名称
        Department department = departmentDao.findByID(student.getDepartmentID(),universityID);
        drivingLicenseImpl.setDepartment(department.getDepartmentName());


        ///把图片读出来
        ///验证图片是否存在

        String visitPath = drivingLicenseImpl.getImage();
        if(FileOperationObject.isExists(visitPath)){
            System.out.println("cun zai");
        }else {
            System.out.println("bu cun zai");
        }
        session.setAttribute("drivingLicenseImpl",drivingLicenseImpl);

        System.out.println("service");
        System.out.println(drivingLicenseImpl);


        return drivingLicenseImpl;
    }





}
